package com.have.blog.picture.controller;

import com.have.blog.model.models.TSystemConfig;
import com.have.blog.picture.constant.MessageConstant;
import com.have.blog.picture.service.FileService;
import com.have.blog.picture.utils.FeignUtil;
import com.have.blog.utils.ResultUtils;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/file")
@Api(value = "文件服务相关接口", tags = {"文件服务相关接口"})
@Slf4j
public class FileController {
    @Autowired
    private FileService fileService;
    @Autowired
    private FeignUtil feignUtil;

    /**
     * 截图上传图片
     * @param file
     * @return
     */
    @ApiOperation(value = "截图上传", notes = "截图上传")
    @PostMapping(value = "/cropperPicture")
    public String cropperPicture(@RequestParam("file") MultipartFile file) {
        ArrayList<MultipartFile> list = new ArrayList<>();
        list.add(file);
        return fileService.cropperPicture(list);
    }

    /**
     * 获取文件的信息接口
     * fileIds 获取文件信息的ids
     * code ids用什么分割的，默认“,”
     *
     * @return
     */
    @ApiOperation(value = "通过fileIds获取图片信息接口", notes = "获取图片信息接口")
    @GetMapping("/getPicture")
    public String getPicture(
            @ApiParam(name = "fileIds", value = "文件ids", required = false) @RequestParam(name = "fileIds", required = false) String fileIds,
            @ApiParam(name = "code", value = "切割符", required = false) @RequestParam(name = "code", required = false) String code) {
        log.info("获取图片信息: {}", fileIds);
        return fileService.getPicture(fileIds, code);
    }


    /**
     * 多文件上传
     * 上传图片接口   传入 userId sysUserId ,有那个传哪个，记录是谁传的,
     * projectName 传入的项目名称如 base 默认是base
     * sortName 传入的模块名， 如 admin，user ,等，不在数据库中记录的是不会上传的
     *
     * @return
     */
    @ApiOperation(value = "多图片上传接口", notes = "多图片上传接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "filedatas", value = "文件数据", required = true),
            @ApiImplicitParam(name = "userUid", value = "用户UID", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sysUserId", value = "管理员UID", required = false, dataType = "String"),
            @ApiImplicitParam(name = "projectName", value = "项目名", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sortName", value = "模块名", required = false, dataType = "String")
    })
    @PostMapping("/pictures")
    public synchronized String uploadPics(HttpServletRequest request, List<MultipartFile> filedatas) {
        // 获取系统配置文件
        TSystemConfig systemConfig = feignUtil.getSystemConfig();
        if (systemConfig == null) {
            return ResultUtils.errorWithMessage(MessageConstant.SYSTEM_CONFIG_IS_NOT_EXIST);
        }
        return fileService.batchUploadFile(request, filedatas, systemConfig);
    }


}
